<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Coupon extends Controller_Index_Base
{ 
    public function action_index()
    {          
        $content = new View('index/coupon/index_tpl');
        $coupons = ORM::factory('coupon')->get_coupons(null, null, null, 1);
        $content->coupons = $coupons;
        $content->shops   = ORM::factory('shop')->get_shops_by_coupons($coupons);
        
        $this->print_template('Каталог бесплатных купонов и промокодов на скидку', $content);
    }
    
    public function action_shop()
    {
        $shop_url = $this->request->param('shop') ? $this->request->param('shop') : null;
        $page     = $this->request->param('page') ? $this->request->param('page') : 1; 
        
        $shop    = ORM::factory('shop')->get_shop_by_url($shop_url);
        if (!$shop->loaded()) {
            throw new Model_Exception('Данного магазина в нашей базе не существует');
        }      
        $coupons = ORM::factory('coupon')->get_coupons(null, null, $shop, $page);       
        
        $content = new View('index/coupon/shop_tpl');
        $content->shop    = $shop; 
        $content->coupons = $coupons;
        
        $count = ORM::factory('coupon')->get_coupons(null, null, $shop, $page, true);
        $content->pagination = array(                                                   
            'page' => $page,
            'per_page' => Model_Coupon::get_coupons_per_page(),
            'count' => $count,
        );

        $title       = 'Промокоды '.$shop->name.', скидки и бесплатные купоны';
        $description = 'Воспользуйтесь бесплатными купонами и промокодами '.$shop->name.', чтобы немного сэкономить';
        $this->print_template($title, $content, $description);
    }
   
    public function action_product_go()
    {
        $coupon_id  = $this->request->param('coupon_id') ? $this->request->param('coupon_id') : 0;
        $product_id = $this->request->param('product_id') ? $this->request->param('product_id') : 0;
        
        $coupon   = ORM::factory('coupon', $coupon_id); 
        $product  = ORM::factory('product', $product_id);
        $shop     = ORM::factory('shop', $product->shop_id); 
        
        $content = new View('index/coupon/product_go_tpl');
        $content->title   = $product->name;
        $content->coupon  = $coupon; 
        $content->product = $product;
        $content->shop    = $shop;

        $this->response->body($content); 
    }
    
    public function action_product_out()
    {
        $coupon_id  = $this->request->param('coupon_id') ? $this->request->param('coupon_id') : 0;
        $product_id = $this->request->param('product_id') ? $this->request->param('product_id') : 0;
        
        $coupon   = ORM::factory('coupon', $coupon_id); 
        $product  = ORM::factory('product', $product_id);
        $shop     = ORM::factory('shop', $product->shop_id); 
        
        ORM::factory('click')->add_click_if_cashback_available($shop);
        
        $content = new View('index/coupon/product_out_tpl');
        $content->coupon  = $coupon; 
        $content->product = $product;
        $content->shop    = $shop;
        $content->delay   = 2;

        $this->response->body($content); 
    }
    
    public function action_go()
    {
        $coupon_id  = $this->request->param('coupon_id') ? $this->request->param('coupon_id') : 0;
        
        $coupon   = ORM::factory('coupon', $coupon_id);
        $shop     = ORM::factory('shop', $coupon->shop_id);          
        
        $content = new View('index/coupon/go_tpl');
        $content->coupon  = $coupon;
        $content->shop    = $shop;   
        
        $this->response->body($content);
    }
    
    public function action_out()
    {
        $coupon_id  = $this->request->param('coupon_id') ? $this->request->param('coupon_id') : 0;
        
        $coupon   = ORM::factory('coupon', $coupon_id);
        $shop     = ORM::factory('shop', $coupon->shop_id); 
        
        ORM::factory('click')->add_click_if_cashback_available($shop);         
        
        $content = new View('index/coupon/out_tpl');
        $content->coupon  = $coupon;
        $content->shop    = $shop; 
        $content->delay   = 2;  
        
        $this->response->body($content);
    }
    
    public function action_product_head()
    {
        $coupon_id  = $this->request->param('coupon_id') ? $this->request->param('coupon_id') : 0;
        $product_id = $this->request->param('product_id') ? $this->request->param('product_id') : 0;   
            
        $coupon   = ORM::factory('coupon', $coupon_id);
        $product  = ORM::factory('product', $product_id); 
       
        $content = new View('index/coupon/product_head_tpl');
        $content->coupon  = $coupon;
        $content->product = $product; 

        $this->response->body($content); 
    }
    
    public function action_head()
    {
        $coupon_id  = $this->request->param('coupon_id') ? $this->request->param('coupon_id') : 0; 
            
        $coupon   = ORM::factory('coupon', $coupon_id);
       
        $content = new View('index/coupon/head_tpl');
        $content->coupon  = $coupon;

        $this->response->body($content); 
    }
} 
